/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package system;

import database.DatabazovaChyba;
import database.DbService;
import gui.DetailyUkolu;
import gui.HlavniOkno;
import gui.PrihlasovaciDialog;
import gui.VytvoreniProjektu;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import ukoly.ProjektControl;
import ukoly.UkolControl;
import ukoly.UkolStatus;
import uzivatele.Uzivatel;

/**
 *
 * @author Pavel Lieberzeit
 */
public class Launcher {

    private static Launcher launcher;
    private DbService database;
    private Uzivatel uzivatel;  //prihlaseny uzivatel
    private HlavniOkno okno;

    private Launcher() {
        this.database = new DbService();
        try {
        } catch (Exception e) {
        }
    }

    public static Launcher getInstance() {

        if (Launcher.launcher == null) {
            Launcher.launcher = new Launcher();
        }

        return Launcher.launcher;

    }

    public void setOknoDoprostred(javax.swing.JFrame okenko) {
        //  Tod jsem to slohnul http://leepoint.net/notes-java/GUI/containers/10windows/15framesize.html
        
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = okenko.getSize();

        int windowX = Math.max(0, (screenSize.width - windowSize.width) / 2);
        int windowY = Math.max(0, (screenSize.height - windowSize.height) / 2);

        okenko.setLocation(windowX, windowY); 

    }

    public void setHlavniOkno(HlavniOkno okno) {
        this.okno = okno;
    }

    /**
     * Tohle volejte pokazdy kdyz v nejakem okne udelate nejake zmeny do databaze
     */
    public void refresh(){
        
        ArrayList<Uzivatel> uAL = null;
        try {
            uAL = this.database.vyberUzivatele("WHERE user_id='" + uzivatel.getId() + "'");
        } catch (DatabazovaChyba ex) {
            JOptionPane.showMessageDialog(okno, "Chyba v komunikaci s databází\n"+ex.getMessage());
        }
        
        if (uAL.isEmpty()) {
            JOptionPane.showMessageDialog(okno, "Byl jste vymazán z databáze.");
            okno.dispose();
            java.awt.EventQueue.invokeLater(new Runnable() {

                @Override
                public void run() {
                    PrihlasovaciDialog prihlasovaciOkno = new PrihlasovaciDialog();
                    prihlasovaciOkno.setVisible(true);
                }
            });
        } else {
            uzivatel = uAL.get(0);
            this.okno.refresh();
        }
    }

    public DbService getDatabase() {
        return database;
    }

    public Uzivatel getUzivatel() {
        return this.uzivatel;
    }

    public void setUzivatel(Uzivatel uzivatel) {
        this.uzivatel = uzivatel;
    }

    public static void main(String args[]) {

        //TODO LAUNCH LOGIN WINDOW! WARP 5, ENGAGE!      

        /** testing code */
        final GregorianCalendar end = new GregorianCalendar();
        end.setTime(new Date(System.currentTimeMillis() + 400000000));
        final GregorianCalendar start = new GregorianCalendar();
        start.setTime(new Date(System.currentTimeMillis()));
//        try {
//            Launcher.getInstance().getDatabase().vytvorUzivatele(new Uzivatel(0, "pes", "haf", "test@mail.com", "nikdo", 0, "pes"));
//        } catch (DatabazovaChyba ex) {
//            Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
//        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
//                new DetailyUkolu(new UkolControl(0, "A", ":(", start, end, end, null, null, UkolStatus.COMPLETE, null, 0)/*/null*/).setVisible(true);
//                new VytvoreniProjektu(new ProjektControl(0, "A", ":(", start, end, end, 0, null, UkolStatus.COMPLETE)/*/null*/).setVisible(true);
                new PrihlasovaciDialog().setVisible(true);
            }
        });

//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
////                new DetailyUkolu(new UkolControl(0, "A", ":(", start, end, end, null, null, UkolStatus.COMPLETE, null, 0)/*/null*/).setVisible(true);
////                new VytvoreniProjektu(new ProjektControl(0, "A", ":(", start, end, end, 0, null, UkolStatus.COMPLETE)/*/null*/).setVisible(true);
//                new HlavniOkno().setVisible(true);
//            }
//        });

    }
}
